//
//  ConfigDevice.swift
//  flowerpot
//
//  Created by fyname on 15/11/16.
//  Copyright © 2015年 senhe. All rights reserved.
//

import UIKit
import CocoaAsyncSocket

class ConfigDeviceController: UIViewController,AsyncUdpSocketDelegate {

    @IBOutlet weak var devSsid: UILabel!
    
    @IBOutlet weak var pwdText: UITextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "设备配置"
        devSsid.text = Common.WIFISSID
        
        // 自定义返回按钮
        let backButton = UIBarButtonItem(title: "く返回", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
        self.navigationItem.leftBarButtonItem = backButton
        
        // 弥补因为返回按钮被替换导致的边缘滑入手势失效的问题
        let gesture = UIPanGestureRecognizer(target: self, action: "goBack")
        self.view.addGestureRecognizer(gesture)
        
        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    
    func goBack() {
//        self.navigationController?.popViewControllerAnimated(true)
//        Common.rootViewController.mainTabBarController.tabBar.hidden = false
        print("configdev")
        Common.rootViewController.mainTabBarController.view.addSubview(Common.indexVC.view)
        Common.rootViewController.mainTabBarController.view.bringSubviewToFront(Common.rootViewController.mainTabBarController.tabBar)
    }
    
    
    @IBAction func ConfigDeviceClick(sender: AnyObject) {
     
        //客户端发送广播信息
        let updClient = AsyncUdpSocket(delegate:self)
        do{
        try updClient.enableBroadcast(true)
        }
        catch _ as NSError {
            log.debug("不能设置广播通知!")
        }
        
        let data = "hello udp".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
        updClient.sendData(data,toHost:"255.255.255.255",port:18266,withTimeout:5000,tag:1)
        bindUdpServer()
    }
    
    func onUdpSocket(sock: AsyncUdpSocket!, didSendDataWithTag tag: Int){
        log.debug("广播数据发送成功!")
    }
    
    func onUdpSocket(sock: AsyncUdpSocket!, didNotSendDataWithTag tag: Int, dueToError error: NSError!){
        log.debug("广播数据发送失败!")
    }
    
    
    func bindUdpServer(){
        let udpServer = AsyncUdpSocket()
        do{
        try  udpServer.bindToPort(7001)
        }catch _ as NSError{
            log.debug("不能设置广播端口!")
        }
        
        udpServer.setDelegate(self)
        udpServer.receiveWithTimeout(5, tag: 1)
        
    }
    
    func onUdpSocket(sock: AsyncUdpSocket!, didReceiveData data: NSData!, withTag tag: Int, fromHost host: String!, port: UInt16) -> Bool{
        let str = NSString(data: data, encoding: NSUTF8StringEncoding)
        print("\(str)")
        return true
    }
    
    func onUdpSocket(sock: AsyncUdpSocket!, didNotReceiveDataWithTag tag: Int, dueToError error: NSError!){
        log.debug("接收广播数据失败!")
    }
    
    
}
